import XLSX from "xlsx";
import export2Excel from "@/util/export2Excel";

export const exportExcel = async (data) => {
  let list = [];
  data && data.length > 0
    ? data.map((item, index) => {
        let topic = {
          题目: `${index + 1}、${item.subjecName}`,
          选项1: item.adds[0] && item.adds[0].answer,
          选项2: item.adds[1] && item.adds[1].answer,
          选项3: item.adds[2] && item.adds[2].answer,
          选项4: item.adds[3] && item.adds[3].answer,
          选项5: item.adds[4] && item.adds[4].answer,
          选项6: item.adds[5] && item.adds[5].answer,
        };
        list.push(topic);
      })
    : list.push(data);

  let worksheet1 = XLSX.utils.json_to_sheet(list);

  let headers = ["A1", "B1", "C1", "D1", "E1", "F1", "G1"];
  headers.forEach((item) => {
    worksheet1[`${item}`].s = {
      font: {
        sz: 14,
        bold: true,
        vertAlign: true,
      },
      fill: {
        bgColor: {
          indexed: 64,
        },
        fgColor: {
          rgb: "C0C0C0",
        },
      },
      alignment: {
        horizontal: "center",
      },
    };
  });

  worksheet1["!cols"] = [
    { wpx: 260 },
    { wpx: 150 },
    { wpx: 150 },
    { wpx: 150 },
    { wpx: 150 },
    { wpx: 150 },
    { wpx: 150 },
  ];

  export2Excel({
    worksheets: {
      sheet1: worksheet1,
    }, // 导出excel的数据，key表示工作表名，value表示对应工作表的 sheet 数据，支持导出多个工作表
    fileName: "量表题目", // 导出文件名
    type: "xlsx", // 文件导出类型
  });
};
